/**
 * Created by json.lu on 2016/3/23.
 */
var mysql = require( '../config/mysqldb' )();
var Q = require( 'q' );
var path = require( 'path' );
var fs = require( 'fs' );
var adminModel = {};

function readFile( previous, fileName ) {
    return Q.promise( function( resolve, reject ) {
        fs.readFile( path.join( process.cwd(), fileName ),
            function( error, text ) {
                if( error ) {
                    reject( new Error( error ) );
                }
                else {
                    resolve( previous + text.toString() );
                }
            } );
    } );
}

adminModel.getUserInfoByUserName = function( username, callback ) {
    mysql.query( 'select * from blog_admin where name=? limit 1', [username], function( error, result ) {
        console.log( error )
        callback( error, result );
    } )
    //mysql.end();
}
function makePromise( args ) {
    var deferred = Q.defer();
    deferred.resolve( 'result ' + args );
    return deferred.promise;
}
adminModel.getUserList = function( callback ) {

    var deferred = Q.defer();
    Q.promise( function( resolve, reject ) {
       /* fs.readFile( path.join( process.cwd(), '1.txt' ),
            function( error, text ) {
                if( error ) {
                    reject( new Error( error ) );
                }
                else {
                   *//* *//*
                    resolve( {'text':text.toString()} );
                }
            } );*/
        mysql.query( 'select * from blog_admin', function( error, result ) {
            resolve(result)
        } )
    } ).then( function( resolve, reject ) {
        console.info(resolve)
        deferred.resolve({'isok':true,'msg':'success'});
        return deferred.promise;
    } ).then(function(resolve,reject){
        console.info(resolve.isok)
        console.info(resolve.msg)
        console.info(resolve)

        return deferred.promise;
    })

    mysql.query( 'select * from blog_admin', function( error, result ) {
        callback( error, result );
    } )
}
module.exports = adminModel;